package com.uha.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uha.reggie.DTO.DishDto;
import com.uha.reggie.common.ReggieResult;
import com.uha.reggie.entity.Category;
import com.uha.reggie.entity.Dish;
import com.uha.reggie.entity.DishFlavor;
import com.uha.reggie.service.CategoryService;
import com.uha.reggie.service.DishFlavorService;
import com.uha.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;


    /**
     * DTO 数据传输对象  当传输过来的数据复杂 实体类接受不了 可以将数据进行封装
     * 传输过来的是JSON数据 加注解 @RequestBody 关联添加 在业务层写相关逻辑
     * @param dishDto dishDto
     * @return  ReggieResult
     */
    @PostMapping
    public ReggieResult<String> save(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        return ReggieResult.success("新增菜品成功~");
    }

    /**
     * MP 分页
     * @param page page
     * @param pageSize pageSize
    //* @param name name
     * @return 没有菜品分类 只有菜品分类Id
     */
    @GetMapping("/page")
    public ReggieResult<Page> page(int page, int pageSize,String name){

        //构造分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records  = pageInfo.getRecords();
// https://www.bilibili.com/video/BV13a411q753/?p=62&spm_id_from=pageDriver&vd_source=d4da531fedb3037f71e4ea859b993e08
        List<DishDto> list = records.stream().map((item)->{
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return ReggieResult.success(dishDtoPage);

    }

    /**
     * 根据Id查询 回显详情
     * @param id id
     * @return null
     */
    @GetMapping("/{id}")
    public ReggieResult<DishDto> get(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return ReggieResult.success(dishDto);
    }



    /**
     * 修改菜品以及口味 关联修改
     * @param dishDto dishDto
     * @return null
     */
    @PutMapping
    public ReggieResult<String> edit(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return  ReggieResult.success("修改菜品成功");
    }




    /**
     * 删除菜品以及口味 关联删除
     * @param ids ids 批量删除
     * @return null
     */
    @DeleteMapping
    public ReggieResult<String> delete(Long[] ids){

        dishService.deleteWithFlavor(ids);

        return  ReggieResult.success("删除成功~");
    }

    /**
     * 套餐页面查菜品列表
     *
     */

    //@GetMapping("/list")
    //public ReggieResult<List<Dish>> LIST(Dish dish){
    //
    //    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
    //
    //    queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
    //    queryWrapper.eq(Dish::getStatus,1);
    //    queryWrapper.orderByDesc(Dish::getSort);
    //
    //    List<Dish> list = dishService.list(queryWrapper);
    //
    //    return ReggieResult.success(list);
    //}
    @GetMapping("/list")
    public ReggieResult<List<DishDto>> LIST(Dish dish){

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByDesc(Dish::getSort);

        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> dishDtoList = list.stream().map((item)->{
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);

            List<DishFlavor> dishFlavors = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());


        return ReggieResult.success(dishDtoList);
    }



    /**
     * 起售停售---批量起售停售
     * @param ids  ids
     * @return null
     */
    @PostMapping("/status/{status}")
    public ReggieResult<String> status(@PathVariable Integer status, Long[] ids){
        Dish dish = new Dish();
        dish.setStatus(status);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();


            queryWrapper.in(Dish::getId,ids);

        dishService.update(dish,queryWrapper);
        return  ReggieResult.success("已更改~");
    }


}
